#pragma once

#include <iostream>
#include <vector>
#include <mutex>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <string>
#include <memory>

struct TCPConnection
{
    int sockfd;
    struct sockaddr_in server_addr;
    bool in_connect;
    bool in_use;
    std::mutex mtx; // 互斥锁，用于保护该连接的状态
};

class TcpConnectionPool
{
public:
    TcpConnectionPool(int pool_size);

    std::shared_ptr<TCPConnection> getConnectionFromPool(std::string server_ip, uint16_t server_port);

    void releaseConnection(std::shared_ptr<TCPConnection> conn);

    ~TcpConnectionPool();

private:
    std::vector<std::shared_ptr<TCPConnection>> connection_pool;
    std::mutex pool_mtx; // 互斥锁，用于保护连接池整体的访问
};